import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

// 导入 store
import store from "@/store"

// 首页
import home from "@/views/home/home.vue";
// 购物车
import shopcart from "@/views/shopcart/shopcart.vue";
// 类别
import category from "@/views/category/category.vue";
// 我的
import user from "@/views/user/user.vue";

// 登录
import login from "@/views/login/login.vue";

// 商品列表
import productList from '../views/productList/productList.vue';

// 商品详情
// import productDetail from '..';


const routes = [
  {
    path: "/",
    name: "Home",
    component: home,
    meta: {
      index: 1
    }
  },
  {
    path: "/shopcart",
    name: "Shopcart",
    component: shopcart,
    meta: {
      index: 1,
      auth: true
    }
  },
  {
    path: "/category",
    name: "Category",
    component: category,
    meta: {
      index: 1
    }
  },
  {
    path: "/user",
    name: "User",
    component: user,
    meta: {
      index: 1,
      auth: true
    }
  },
  {
    path: "/login",
    name: "Login",
    component: login,
    meta: {
      index: 2,
    }
  },
  {
    path: "/product-list",
    name: "productList",
    component: productList,
    meta: {
      index: 2
    }
  },
  {
    path: "/detail/:id",
    name: "Detail",
    component: () => import('@/views/productDetail/productDetail.vue'),
    meta: {
      index: 3
    }
  },
  {
    path:'/proxy',
    name: 'Proxy',
    component: () => import('@/views/login/proxy.vue')
  }
];

const router = new VueRouter({
  routes,
});


router.beforeEach((to, from, next) => {
  if (to.name === 'Login' && store.getters.getIsLogin) {
    next('/user')
  }

  // 需要校验用户是否登录的页面
  if (to.meta.auth) {

    if (store.getters.getIsLogin) {
      next()
    } else {
      // 未登录 跳转到登录页面
      next({ path: '/login' })
    }
  } else {
    next()
  }
})

export default router;
